from big_ol_pile_of_manim_imports import *


class Shapes(Scene):

    CONFIG = {
        'set_color':RED,
    }

    def getMid(self,p1, p2):
        n1 = (p1[0] + p2[0]) / 2
        n2 = (p1[1] + p2[1]) / 2
        return np.array([n1,n2,0])

    def recursion(self,degree,points):
        colormap = [BLUE, RED, GREEN, WHITE, YELLOW, ORANGE]

        self.write(points)
        if degree > 0 :
            self.recursion(degree-1,{
                'pl':points['pl'],
                'pu':self.getMid(points['pl'],points['pu']),
                'pr':self.getMid(points['pl'],points['pr'])
            })
            self.recursion(degree-1,{
                'pl':self.getMid(points['pl'],points['pu']),
                'pu':points['pu'],
                'pr':self.getMid(points['pu'],points['pr'])
            })
            self.recursion(degree - 1, {
                'pl': self.getMid(points['pl'], points['pr']),
                'pu': self.getMid(points['pu'], points['pr']),
                'pr': points['pr']
            })
        else:
            return

    def write(self,points):
        tri = Polygon(points['pl'], points['pu'], points['pr']).set_color(WHITE)
        print(type(tri))
        self.play(FadeIn(tri),run_time=0.03)

    def construct(self):

        points = {
            'pl':np.array([-3,-2,0]),
            'pr':np.array([3,-2,0]),
            'pu':np.array([0,3,0]),
        }

        tri = Polygon(points['pl'],points['pu'],points['pr']).set_color(WHITE)
        self.play(Write(tri))
        degree = 5
        self.recursion(degree,points)
        self.wait()
        group = Group(*self.mobjects)


        # circle = Circle()
        # circle.surround(group)
        # text = TextMobject('三角形分形').set_height(1.3).scale(0.4).shift(0.5*DOWN)
        #
        # self.play(
        #     Write(text)
        # )

        self.wait(3)


